<template>
  <div class="shopcar">
    <div class="mui-card" v-for="(item,i) in goodlist" :key="item.id">
      <div class="mui-card-content">
        <div class="mui-card-content-inner goodsitem">
          <mt-switch
            v-model="$store.getters.getGoodsSelected[item.id]"
            @change="changeSelect(item.id,$store.getters.getGoodsSelected[item.id])"
          ></mt-switch>
          <div class="img">
            <img :src="item.thumb_path" alt>
          </div>
          <div class="right">
            <div class="tit">{{item.title}}</div>
            <div class="right-bom">
              <span>{{item.sell_price}}</span>
              <numbox :getItemCount="$store.getters.getGoodsCount[item.id]" :itemid="item.id"></numbox>
              <a @click.prevent="remove(item.id, i)">删除</a>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="mui-card">
      <!--内容区-->
      <div class="mui-card-content total">
        <p>总计不含运费</p>
        <div class="bottom">
          <div>已勾选<span class="red">{{$store.getters.getAllCount}}</span>件 总价<span class="red">{{$store.getters.getAlltotal}}</span></div>
          <mt-button type="danger" size="samll">去结算</mt-button>
        </div>
      </div>
    </div>
    <p>{{ $store.getters.getGoodsSelected }}</p>
  </div>
</template>

<script>
import numbox from "../subcomponents/shopcar_numbox.vue";
export default {
  data() {
    return {
      goodlist: []
    };
  },
  created() {
    this.getGoodList();
  },
  methods: {
    getGoodList() {
      var ids = this.$store.getters.getAllId;
      if (ids.length <= 0) {
        return;
      } else {
        this.$http
          .get("api/goods/getshopcarlist/" + ids.join(","))
          .then(result => {
            if (result.body.status == 0) {
              this.goodlist = result.body.message;
            } else {
              console.log("获取失败");
            }
          });
      }
    },
    remove(id, index) {
      // 点击删除，把商品从 store 中根据 传递的 Id 删除，同时，把 当前组件中的 goodslist 中，对应要删除的那个商品，使用 index 来删除
      this.goodlist.splice(index, 1);
      this.$store.commit("removeGoods", id);
    },
    changeSelect(id, val) {
      //点击开关之后吧当前的状态同步到store中去
      this.$store.commit("updateGoodsSelected", {
        id: id,
        selected: val
      });
    }
  },
  components: {
    numbox
  }
};
</script>

<style lang="scss" scoped>
.img {
  width: 60px;
  img {
    width: 100%;
  }
}
.goodsitem {
  display: flex;
}
.total .bottom{
  display: flex;
  color: #999;
  justify-content: space-between;
  .red{
    color: #f40;
    font-weight: bolder;
    justify-content: space-between;
  }
}
</style>
